package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> savePage(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
    //创建分页对象
    IPage<Emp> iPage=new Page<>(page,pageSize);
    //创建条件构造器
    LambdaQueryWrapper<Emp> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.like(name!=null&& !name.isEmpty(),Emp::getName,name).eq(gender!=null,Emp::getGender,gender).between(begin!=null&&end!=null,Emp::getEntryDate,begin,end).orderByDesc(Emp::getUpdateTime);
    //调用分页方法
    this.page(iPage,queryWrapper);
    //获取查询到的数据
        List<Emp> records = iPage.getRecords();//查询到的集合
        long total = iPage.getTotal();//总条数
    //判断查询到的集合是否为空
        if (!CollectionUtils.isEmpty(records)){
            //不为空,获取员工的部门id
            List<Integer> deptIds = records.stream().map(Emp::getDeptId).toList();
            //去重
            deptIds=deptIds.stream().distinct().toList();
            //获取部门集合
            List<Dept> depts = deptMapper.selectByIds(deptIds);
            //封装员工部门名称
            records.forEach(emp->{
                depts.stream().filter(dept -> dept.getId().equals(emp.getDeptId())).findFirst().ifPresent(dept -> emp.setDeptName(dept.getName()));
            });

        }

        return new PageResult<>(total,records);
    }

    @Override
    @Transactional
    public void add(Emp emp) {
        //补全员工信息
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        emp.setPassword("123456");
        //添加员工的基本信息
        this.save(emp);
        //判断员工的工作经历是否为空,不为空添加工作经历
        if (!CollectionUtils.isEmpty(emp.getExprList())){
            List<EmpExpr> exprList = emp.getExprList();
            exprList.stream().forEach(expr->{
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public Emp selectById(Integer id) {
        //查询员工的基本信息
        Emp emp = empMapper.selectById(id);
        //根据员工id查询员工经历
        LambdaQueryWrapper<EmpExpr> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId,emp.getId());
        //封装员工工作经历
        emp.setExprList(empExprMapper.selectList(queryWrapper));
        return emp;
    }

    @Override
    public void updateEmpById(Emp emp) {
        //修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //删除原有的工作经历
        LambdaQueryWrapper<EmpExpr> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprMapper.delete(queryWrapper);
        //判断修改后的工作经历是否为空
        if (!CollectionUtils.isEmpty(emp.getExprList())){
            //获取更改后员工经历
            List<EmpExpr> exprList = emp.getExprList();
            //补全工作经历中的员工id
            exprList.stream().forEach(expr->{
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }

    }

    @Override
    @Transactional
    public void delect(List<Integer> ids) {
        //删除员工的基本信息
        empMapper.deleteByIds(ids);
        //删除员工的工作经历
        LambdaQueryWrapper<EmpExpr> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(EmpExpr::getEmpId,ids);
        empExprMapper.delete(queryWrapper);
    }
}
